﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using PagedList;
using CubicFWNet.Common;

namespace CubicFWNet.Mvc.Controllers
{
    public class ProductController : ControllerBase
    {
        private ProductApp _productApp;
        private InventoryApp _inventoryApp = new InventoryApp();
        private InventoryUerApp _inventoryUerApp = new InventoryUerApp();
        private TakeLogApp _takeLogApp = new TakeLogApp();
        private CategoryApp _categoryApp = new CategoryApp();
        public ProductController()
        {
            _productApp = new ProductApp();
        }

        /// <summary>
        /// 产品查询
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int page = 1, int categoryid = 0, string productName = "", string width = "", string length = "")
        {
            productName = productName.Trim();
            ViewBag.ProductName = productName;
            var category = _categoryApp.GetList();
            category.Insert(0, new Category() { Id = 0, Name = "全部" });
            ViewBag.CategoryList = category;
            ViewBag.Width = width;
            ViewBag.Length = length;
            ViewBag.CategoryId = categoryid;
            IPagedList<ProductView> productList = _productApp.GetList1(categoryid, productName, width, length).ToPagedList(page, 10);
            return View(productList);
        }

        /// <summary>
        /// 产品列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ListPro(int page = 1, int categoryid = 0, string productName = "")
        {
            ViewBag.ProductName = productName;

            IPagedList<ProductView> productList = _productApp.GetList(categoryid, productName).ToPagedList(page, 10);
            return View(productList);
        }

        /// <summary>
        /// 产品信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Info()
        {
            string pid = Request.QueryString["PID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (pro != null)
            {
                ViewBag.Product = pro;
            }
            List<TakeLog> lists = _takeLogApp.GetListByProductId(id);
            if (lists != null)
            {
                ViewBag.TakeLogs = lists;
            }
            return View();
        }

        /// <summary>
        /// 产品添加
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime end = DateTime.Now.AddDays(1);

            //生产唯一编号
            //int count = _productApp.GetProductCount(operatorModel.UserId);
            //count += 1;
            //string tmp = count.ToString();
            //if (count < 10)
            //{
            //    tmp = "0000" + count;
            //}
            //else if (count < 100)
            //{
            //    tmp = "000" + count;
            //}
            //else if (count < 1000)
            //{
            //    tmp = "00" + count;
            //}
            //else if (count < 10000)
            //{
            //    tmp = "0" + count;
            //}
            //ViewBag.Number = operatorModel.ShortName + "-" + tmp;

            //获取产品分类
            ViewBag.CategoryList = _categoryApp.GetList();

            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 产品添加[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(Product model)
        {
            model.Id = Guid.NewGuid();
            model.CreateTime = DateTime.Now;
            model.CreatorId = operatorModel.UserId;
            model.CreatorName = operatorModel.FamilyName;

            int i = _productApp.Insert(model);
            if (i == 1)
            {
                return RedirectToAction("Index", "Product");
            }
            else
            {
                ViewBag.Info = "抱歉，添加失败！";
            }

            ViewBag.RealName = operatorModel.FamilyName;
            return View();
        }

        /// <summary>
        /// 产品编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            string pid = Request.QueryString["PID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (pro != null)
            {
                ViewBag.Product = pro;
            }
            ViewBag.RealName = pro.CreatorName;
            ViewBag.CategoryList = _categoryApp.GetList();
            return View();
        }

        /// <summary>
        /// 产品编辑[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Product model, Guid id)
        {
            //string pid = productId;
            //Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (null != pro)
            {
                //pro.Name = fc["ProductName"];
                //pro.Number = fc["Number"];
                //pro.Format = fc["Format"];
                //pro.Memo = fc["Memo"];
                //pro.Unit = fc["Unit"];

                pro.Name = model.Name;
                pro.Number = model.Number;
                pro.Format = model.Format;
                pro.Memo = model.Memo;
                pro.CategoryId = model.CategoryId;
                pro.Unit = model.Unit;

                pro.Price = model.Price;
                pro.SizeWidth = model.SizeWidth;
                pro.SizeLenth = model.SizeLenth;

                int i = _productApp.Update(pro);



                if (i == 1)
                {
                    return RedirectToAction("Index", "Product");
                }
                else
                {
                    ViewBag.Info = "抱歉，提交失败！";
                }
            }
            ViewBag.RealName = pro.CreatorName;
            return View();
        }

        /// <summary>
        /// 修改库存信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditProductPrice(Guid pid, decimal price)
        {
            //更新产品总库存
            Product pro = _productApp.GetInfo(pid);
            if (null != pro)
            {
                pro.Price = price;
                int i = _productApp.Update(pro);
                if (i == 1)
                {
                    return Json("修改内部价格成功");
                }
                else
                {
                    return Json("抱歉，提交失败！");
                }
            }
            return Json("抱歉，提交失败！");
        }

        /// <summary>
        /// 产品列表[采购修改价格]
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangeList(int page = 1, string productName = "")
        {
            ViewBag.ProductName = productName;
            //ViewBag.ProductNum = productNum;

            var category = _categoryApp.GetList();
            category.Insert(0, new Category() { Id = 0, Name = "全部" });
            ViewBag.CategoryList = category;

            IPagedList<ProductView> productList = _productApp.GetList(0, productName).ToPagedList(page, 10);
            return View(productList);
        }

        /// <summary>
        /// 修改价格
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePrice()
        {
            string pid = Request.QueryString["PID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (pro != null)
            {
                ViewBag.Product = pro;
            }
            ViewBag.RealName = pro.CreatorName;
            return View();
        }

        /// <summary>
        /// 修改价格[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangePrice(FormCollection fc)
        {
            string pid = fc["ProductID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (null != pro)
            {
                pro.Memo = fc["Memo"];

                pro.Price = Convert.ToDecimal(fc["Price"]);

                int i = _productApp.Update(pro);
                if (i == 1)
                {
                    return RedirectToAction("ChangeList", "Product");
                }
                else
                {
                    ViewBag.Info = "抱歉，提交失败！";
                }
            }
            ViewBag.RealName = pro.CreatorName;
            return View();
        }

        /// <summary>
        /// 库存查询
        /// </summary>
        /// <returns></returns>
        public ActionResult InventoryList(int page = 1, int storageId = 0, int categoryId = 0, string name = "")
        {
            IEnumerable<InventoryView> ls = _inventoryApp.GetInventoryList(categoryId, storageId, name).ToPagedList(page, 10);
            //foreach (var inv in ls)
            //{
            //    inv.Amount = _inventoryUerApp.GetListByProduct(new Guid(inv.ProuctId.ToString())).Sum(a => a.Amount);
            //}
            ViewBag.Name = name;
            return View(ls);
        }

        /// <summary>
        /// 订单库存列表
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderInventory()
        {
            string str = Request.QueryString["PID"];
            List<InventoryUserView> ls = _inventoryUerApp.GetListByProduct(new Guid(str)).ToList();
            ViewBag.NoOrder = GetNewOrderRelationView();
            return View(ls);
        }
        private OrderRelationView GetNewOrderRelationView()
        {
            OrderRelationView orv = new OrderRelationView();
            orv.OrderId = new Guid();
            orv.Number = "0";
            orv.CustomeId = new Guid();
            orv.CustomerName = "无";
            orv.UserId = new Guid();
            orv.UserName = "公共库存，与销售(订单)无关联";
            orv.ProductName = "无";
            orv.CusProName = "无";

            return orv;
        }
    }
}